var app = {};
var color = [
    "#2ec7c9",
    "#b6a2de",
    "#5ab1ef",
    "#ffb980",
    "#d87a80",
    "#8d98b3",
    "#e5cf0d",
    "#97b552",
    "#95706d",
    "#dc69aa",
    "#07a2a4",
    "#9a7fd1",
    "#588dd5",
    "#f5994e",
    "#c05050",
    "#59678c",
    "#c9ab00",
    "#7eb00a",
    "#6f5553",
    "#c14089"
];

var bmap_def = function (str, res) {
    bmap = {
        center: [res[0]["longitude"], res[0]["latitude"]],
        zoom: 17.5,
        roam: true,
    }
    resData = 0
    for (var i = 0; i < res.length; i++) {
        name = res[i]["name"];
        like_num = strSimilarity2Percent(str, name)
        if (resData < like_num) {
            resData = like_num;
            bmap["center"] = [res[i]["longitude"], res[i]["latitude"]]
        }
    }

    return bmap;
};

/*
 * 遍历有评分并且是长沙的数组
 */
var aMapdataList = function (res) {
    resList = []
    for (var i = 0; i < res.length; i++) {
        //				console.log("\t"+i+"\t"+(res[i]["rating"] != "") + "\t" + (res[i]["adcode"].substr(0, 3) == "430") )
        if (res[i]["rating"] != "" && res[i]["adcode"].substr(0, 3) == "430") {
            bd_encrypt_d = bd_encrypt(res[i]["longitude"], res[i]["latitude"])
            resList.push({
                name: res[i]["name"],
                longitude: bd_encrypt_d[0],
                latitude: bd_encrypt_d[1]
            })
        }
    }
    return resList
}
//		console.log(poi_list)
//		console.log(aMapdataList(poi_list))
var series_none_def = function (res) {
    series_none = {
        name: res[0]["name"],
        type: 'scatter',
        coordinateSystem: 'bmap',
//		symbol: 'circle',
        symbol: res[0]["url"],
        data: res,
        encode: {
            value: 2
        },
        symbolSize: function (val) {
            return val[2] * 5;
        },
        label: {
            formatter: '{b}',
            position: 'right'
        },
        //				itemStyle: {
        //					color: color
        //				},
        emphasis: {
            label: {
                show: true
            }
        }
    }
    return series_none;
};
var series_for = function (res) {
    for (var i = 0; i < res.length; i++) {
        //				console.log(res[i]['value'][0] + "\t" + res[i]['value'][1])
        //				console.log(Math.floor(Math.random() * 10) * 0.0001)
        res[i]['value'][0] = res[i]['value'][0] - 0.0005 + Math.floor(Math.random() * 10) * 0.0001;
        res[i]['value'][1] = res[i]['value'][1] - 0.0005 + Math.floor(Math.random() * 10) * 0.0001;
        //				console.log(res[i]['value'][0] + "\t" + res[i]['value'][1])
    }
    return res;
};
var series_def = function (res) {
    series = []
    for (var i = 0; i < res.length; i++) {
        name = res[i]["name"];
        longitude = res[i]["longitude"];
        latitude = res[i]["latitude"];
        resdata = [{
            name: name,
            value: [longitude, latitude, 10],
            url: "image://http://127.0.0.1:8081/assets/images/pot" + (i + 1) + ".gif"
        }]
        series.push(series_none_def(resdata))
    }

    return series
};

var option_def = function (str, listdata) {
    option = {
        color: [
            "#2ec7c9",
            "#b6a2de",
            "#5ab1ef",
            "#ffb980",
            "#d87a80",
            "#8d98b3",
            "#e5cf0d",
            "#97b552",
            "#95706d",
            "#dc69aa",
            "#07a2a4",
            "#9a7fd1",
            "#588dd5",
            "#f5994e",
            "#c05050",
            "#59678c",
            "#c9ab00",
            "#7eb00a",
            "#6f5553",
            "#c14089"
        ],
        backgroundColor: 'transparent',
        tooltip: {
            trigger: 'item'
        },
        bmap: bmap_def(str, aMapdataList(listdata)),
        series: series_def(aMapdataList(listdata)),
    };
    return option
}

var echarts_select = function (id, str, res) {
    console.log(id, str, res)
    var dom = document.getElementById(id);
    var myChart = echarts.init(dom);
    var poi_list = res["data"]["poi_list"];
    option = option_def(str, poi_list);
    if (option && typeof option === "object") {
        console.log(option)
        myChart.setOption(option, true);
    }
}